/**
 * Created by Administrator on 2017/12/21.
 */
/**
 * 栏目管理初始化
 */
var ChannelItem = {
    id: "ChannelItemTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
ChannelItem.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true},
        {title: 'id', field: 'id', align: 'center', valign: 'middle'},
        {title: '标题', field: 'title', align: 'center', valign: 'middle', sortable: true},
        {title: '视频名称', field: 'name', align: 'center', valign: 'middle', sortable: true},
        {title: '所属专辑', field: 'cateName', align: 'center', valign: 'middle', sortable: true},
        {title: '所属年龄', field: 'ageType', align: 'center', valign: 'middle', sortable: true},
        {title: '发布者', field: 'uploader', align: 'center', valign: 'middle', sortable: true},
        {title: '发布时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true},
        {title: '播放量', field: 'playCount', align: 'center', valign: 'middle', sortable: true},
        {title: '分享量', field: 'shareCount', align: 'center', valign: 'middle', sortable: true},
        {title: '下载量', field: 'downloadCount', align: 'center', valign: 'middle', sortable: true},
        {title: '推荐排序值', field: 'recRate', align: 'center', valign: 'middle', sortable: true},
        {title: '推荐指数', field: 'rating', align: 'center', valign: 'middle', sortable: true}
    ];
};

/**
 * 检查是否选中
 */
ChannelItem.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        ChannelItem.seItem = selected[0];
        if(ChannelItem.seItem == 'undefined' || ChannelItem.seItem == '' || ChannelItem.seItem == null) {
            ChannelItem.seItem = selected[1];
        }
        return true;
    }
};
ChannelItem.batchCheck = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        ChannelItem.seItem = selected;
        return true;
    }
};

/**
 * 点击向频道中添加音频资源
 */
ChannelItem.openAddChannelItem = function () {
    var index = layer.open({
        type: 2,
        title: '资源添加',
        area: ['900px', '600px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/channel/item/addPage'
    });
    this.layerIndex = index;
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
ChannelItem.formParams = function() {
    var queryData = {};

    queryData['keyword'] = $("#keyword").val();
    return queryData;
};

/**
 * 查询日志列表
 */
ChannelItem.search = function () {
    ChannelItem.table.refresh({query: ChannelItem.formParams()});
};

ChannelItem.resetSearch = function () {
    $("#keyword").val("");
    ChannelItem.search();
}

/**
 * 删除栏目
 */
ChannelItem.delete = function () {
    if (this.batchCheck()) {

        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/channel/item/delete", function (data) {
                Feng.success("删除成功!");
                ChannelItem.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            var ids = '';
            for (var i = 0; i < ChannelItem.seItem.length;i++) {
               if(i < ChannelItem.seItem.length - 1) {
                   ids += ChannelItem.seItem[i].id+",";
               } else {
                   ids += ChannelItem.seItem[i].id;
               }
            }
            ajax.set("channelResIds", ids);
            ajax.start();
        };

        var names = "";
        for (var i = 0; i < ChannelItem.seItem.length;i++) {
            if(names.indexOf(ChannelItem.seItem[i].title) == -1) {
                if(i < ChannelItem.seItem.length - 1) {
                    names += ChannelItem.seItem[i].title+" ";
                } else {
                    names += ChannelItem.seItem[i].title;
                }
            }
        }
        Feng.confirm("是否删除 " + names + "的关联资源?", operation);
    }
};

$(function () {
    var defaultColunms = ChannelItem.initColumn();
    var table = new BSTable(ChannelItem.id, "/channel/item/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(ChannelItem.formParams());
    ChannelItem.table = table.init();
});

